package com.tancire.member.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import com.tancire.common.utils.DateUtils;
import com.tancire.member.domain.Member;
import com.tancire.member.domain.MemberTrainAward;
import com.tancire.member.service.IMemberService;
import org.springframework.stereotype.Service;
import com.tancire.member.mapper.MemberTrainProblemMapper;
import com.tancire.member.domain.MemberTrainProblem;
import com.tancire.member.service.IMemberTrainProblemService;

/**
 * 会员错题本Service业务层处理
 *
 * @author tancire
 * @date 2024-10-23
 */
@Service
public class MemberTrainProblemServiceImpl implements IMemberTrainProblemService {

    @Resource
    private MemberTrainProblemMapper memberTrainProblemMapper;

    @Resource
    private IMemberService memberService;

    /**
     * 查询会员错题本
     *
     * @param id 会员错题本主键
     * @return 会员错题本
     */
    @Override
    public MemberTrainProblem selectMemberTrainProblemById(Long id) {
        return memberTrainProblemMapper.selectMemberTrainProblemById(id);
    }

    /**
     * 查询会员错题本列表
     *
     * @param memberTrainProblem 会员错题本
     * @return 会员错题本
     */
    @Override
    public List<MemberTrainProblem> selectMemberTrainProblemList(MemberTrainProblem memberTrainProblem) {
        return memberTrainProblemMapper.selectMemberTrainProblemList(memberTrainProblem);
    }

    /**
     * 查询列表数据，给后台用，查的全
     *
     * @param memberTrainProblem
     * @return
     */
    @Override
    public List<MemberTrainProblem> selectList(MemberTrainProblem memberTrainProblem) {
        List<MemberTrainProblem> memberTrainProblems = this.memberTrainProblemMapper.selectMemberTrainProblemList(memberTrainProblem);
        if (ObjectUtil.isNotEmpty(memberTrainProblems)) {
            List<Long> ids = memberTrainProblems.stream().map(MemberTrainProblem::getMemberId).collect(Collectors.toList());
            Map<Long, String> memberMap = memberService.selectMemberMapByIds(ids);
            for (MemberTrainProblem problem : memberTrainProblems) {
                if (null != problem) {
                    problem.setMemberName(memberMap.get(problem.getMemberId()));
                }
            }
        }
        return memberTrainProblems;
    }

    /**
     * 新增会员错题本
     *
     * @param memberTrainProblem 会员错题本
     * @return 结果
     */
    @Override
    public int insertMemberTrainProblem(MemberTrainProblem memberTrainProblem) {
        memberTrainProblem.setCreateTime(DateUtils.getNowDate());
        return memberTrainProblemMapper.insertMemberTrainProblem(memberTrainProblem);
    }

    /**
     * 修改会员错题本
     *
     * @param memberTrainProblem 会员错题本
     * @return 结果
     */
    @Override
    public int updateMemberTrainProblem(MemberTrainProblem memberTrainProblem) {
        memberTrainProblem.setUpdateTime(DateUtils.getNowDate());
        return memberTrainProblemMapper.updateMemberTrainProblem(memberTrainProblem);
    }

    /**
     * 批量删除会员错题本
     *
     * @param ids 需要删除的会员错题本主键
     * @return 结果
     */
    @Override
    public int deleteMemberTrainProblemByIds(Long[] ids) {
        return memberTrainProblemMapper.deleteMemberTrainProblemByIds(ids);
    }

    /**
     * 删除会员错题本信息
     *
     * @param id 会员错题本主键
     * @return 结果
     */
    @Override
    public int deleteMemberTrainProblemById(Long id) {
        return memberTrainProblemMapper.deleteMemberTrainProblemById(id);
    }
}
